//
//  LoginViewController.swift
//  FrameProduct
//
//  Created by delegate on 2025/10/20.
//

import UIKit

class LoginViewController: BaseViewController {
    private var totalTime: Int = 60
    private var timer: Timer?
    private var isUsername = true
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupNavi()
        
        setupUI()
        
    }
    private func setupNavi() {
        navigationItem.title = "登录账号"
    }
    private func setupUI() {
        [userNameButton, phoneButton, blueLineView, userNameTextField, passwordTextField, registerButton, forgetThePasswordButton, loginButton, touristModeButton].forEach { subView in
            view.addSubview(subView)
        }
        userNameButton.snp.makeConstraints { make in
            make.top.equalTo(48)
            make.left.equalTo(0)
            make.height.equalTo(25)
        }
        phoneButton.snp.makeConstraints { make in
            make.left.equalTo(userNameButton.snp.right)
            make.centerY.equalTo(userNameButton.snp.centerY)
            make.height.equalTo(userNameButton.snp.height)
            make.width.equalTo(userNameButton.snp.width)
            make.right.equalToSuperview()
        }
        blueLineView.snp.makeConstraints { make in
            make.centerX.equalTo(userNameButton.snp.centerX)
            make.top.equalTo(userNameButton.snp.bottom).offset(5)
            make.width.equalTo(60)
            make.height.equalTo(2)
        }
        userNameTextField.snp.makeConstraints { make in
            make.top.equalTo(blueLineView.snp.bottom).offset(23)
            make.left.equalTo(22)
            make.right.equalTo(-22)
            make.height.equalTo(60)
        }
        passwordTextField.snp.makeConstraints { make in
            make.top.equalTo(userNameTextField.snp.bottom).offset(30)
            make.left.equalTo(22)
            make.right.equalTo(-22)
            make.height.equalTo(60)
        }
        registerButton.snp.makeConstraints { make in
            make.top.equalTo(passwordTextField.snp.bottom).offset(8)
            make.left.equalTo(passwordTextField.snp.left).offset(9)
            make.height.equalTo(28)
        }
        forgetThePasswordButton.snp.makeConstraints { make in
            make.top.equalTo(passwordTextField.snp.bottom).offset(8)
            make.right.equalTo(passwordTextField.snp.right).offset(-1)
            make.height.equalTo(28)
        }
        loginButton.snp.makeConstraints { make in
            make.top.equalTo(registerButton.snp.bottom).offset(32)
            make.left.equalTo(22)
            make.right.equalTo(-22)
            make.height.equalTo(50)
        }
        touristModeButton.snp.makeConstraints { make in
            make.bottom.equalTo(-ScreenSize.safeAreaInsets.bottom)
            make.centerX.equalToSuperview()
            make.height.equalTo(44)
        }
    }
    //=================================================================
    //                           事件处理
    //=================================================================
    // MARK: - 事件处理
    @objc private func userNameButtonAction() {
        
        isUsername = true
        
        userNameButton.isSelected = true
        phoneButton.isSelected = false
        blueLineView.snp.remakeConstraints { make in
            make.centerX.equalTo(userNameButton.snp.centerX)
            make.top.equalTo(userNameButton.snp.bottom).offset(5)
            make.width.equalTo(60)
            make.height.equalTo(2)
        }
        
        let leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 60))
        userNameTextField.leftView = leftView
        userNameTextField.rightView = leftView
        
        let rightView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 60))
        passwordTextField.leftView = rightView
        passwordTextField.rightView = rightView
    }
    @objc private func phoneButtonAction() {
        
        isUsername = false
        
        userNameButton.isSelected = false
        phoneButton.isSelected = true
        blueLineView.snp.remakeConstraints { make in
            make.centerX.equalTo(phoneButton.snp.centerX)
            make.top.equalTo(userNameButton.snp.bottom).offset(5)
            make.width.equalTo(60)
            make.height.equalTo(2)
        }
        userNameTextField.leftView = countryNumBgView
        let userNameTextFieldRightView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 60))
        userNameTextField.rightView = userNameTextFieldRightView


        let leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 60))
        passwordTextField.leftView = leftView
        passwordTextField.rightView = passwordTextFieldRightView
        
        view.layoutIfNeeded()
    }
    @objc private func registerButtonAction() {
        let vc = RegisterViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
    @objc private func forgetThePasswordButtonAction() {
        let vc = ForgetThePasswordViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
    @objc private func loginButtonAction() {
        print("登录")
        if isUsername {
            let isUsernameEmpty = userNameTextField.text?.isEmpty ?? false
            let isPasswordEmpty = passwordTextField.text?.isEmpty ?? false
            
            if isUsernameEmpty {
                Toast.with(message: "请输入用户名").showOnWindow()
                return
            }
            if isPasswordEmpty {
                Toast.with(message: "请输入密码").showOnWindow()
                return
            }
            
                let vc = MainHomePageViewController()
                navigationController?.pushViewController(vc, animated: true)
        }else {
            let isUsernameEmpty = userNameTextField.text?.isEmpty ?? false
            let isPasswordEmpty = passwordTextField.text?.isEmpty ?? false
            if isUsernameEmpty {
                Toast.with(message: "请输入手机号").showOnWindow()
                return
            }
            if isPasswordEmpty {
                Toast.with(message: "请输入密码").showOnWindow()
                return
            }
            if !isUsernameEmpty && !isPasswordEmpty {
                let vc = MainHomePageViewController()
                navigationController?.pushViewController(vc, animated: true)
            }
        }
        
    }
    @objc private func getNumButtonAction() {
        print("获取验证码")
        getNumButton.isUserInteractionEnabled = false
        timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { [weak self] timer in
            if let number = self?.totalTime, number > 0 {
                self?.getNumButton.setTitle("\(number)s", for: .normal)
                self?.getNumButton.setTitle("\(number)s", for: .selected)
                self?.totalTime -= 1
            }else {
                self?.getNumButton.setTitle("获取验证码", for: .normal)
                self?.getNumButton.setTitle("获取验证码", for: .selected)
                self?.timer?.invalidate()
                self?.timer = nil
                self?.getNumButton.isUserInteractionEnabled = true
            }
        })
    }
    @objc private func touristModeButtonAction() {
        let vc = MainHomePageViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
    deinit {
        print("销毁")
    }
    //=================================================================
    //                            lazy
    //=================================================================
    // MARK: - lazy
    private lazy var userNameButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitle("用户名/邮箱登录", for: .normal)
        button.setTitle("用户名/邮箱登录", for: .selected)
        button.setTitleColor(.lxs.hex("#808080"), for: .normal)
        button.setTitleColor(.lxs.hex("#FFFFFF"), for: .selected)
        button.titleLabel?.font = .lxs.font(size: 16)
        button.addTarget(self, action: #selector(userNameButtonAction), for: .touchUpInside)
        button.isSelected = true
        return button
    }()
    private lazy var phoneButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitle("手机号登录", for: .normal)
        button.setTitle("手机号登录", for: .selected)
        button.setTitleColor(.lxs.hex("#808080"), for: .normal)
        button.setTitleColor(.lxs.hex("#FFFFFF"), for: .selected)
        button.titleLabel?.font = .lxs.font(size: 16)
        button.addTarget(self, action: #selector(phoneButtonAction), for: .touchUpInside)
        button.isSelected = false
        return button
    }()
    private lazy var blueLineView: UIView = {
        let lineView = UIView()
        lineView.backgroundColor = .lxs.hex("#57A0FF")
        return lineView
    }()
    private lazy var userNameTextField: UITextField = {
        let textField = UITextField()
        textField.attributedPlaceholder = NSAttributedString(string: "请输入账号", attributes: [NSAttributedString.Key.foregroundColor : UIColor.lxs.hex("#808080"), NSAttributedString.Key.font: UIFont.lxs.font(size: 14)])
        textField.textColor = .lxs.hex("#FFFFFF")
        textField.font = .lxs.font(size: 14)
        textField.layer.cornerRadius = 10
        textField.layer.masksToBounds = true
        textField.backgroundColor = .lxs.hex("#152443")
        let leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 60))
        textField.leftView = leftView
        textField.leftViewMode = .always
        textField.rightView = leftView;
        textField.rightViewMode = .always
        return textField
    }()
    private lazy var passwordTextField: UITextField = {
        let textField = UITextField()
        textField.attributedPlaceholder = NSAttributedString(string: "请输入密码", attributes: [NSAttributedString.Key.foregroundColor : UIColor.lxs.hex("#808080"), NSAttributedString.Key.font: UIFont.lxs.font(size: 14)])
        textField.textColor = .lxs.hex("#FFFFFF")
        textField.font = .lxs.font(size: 14)
        textField.layer.cornerRadius = 10
        textField.layer.masksToBounds = true
        textField.backgroundColor = .lxs.hex("#152443")
        let leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 60))
        textField.leftView = leftView
        textField.leftViewMode = .always
        textField.rightView = leftView
        textField.rightViewMode = .always
        return textField
    }()
    private lazy var registerButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitle("注册账号", for: .normal)
        button.setTitleColor(.lxs.hex("#FFFFFF"), for: .normal)
        button.titleLabel?.font = .lxs.font(size: 12)
        button.addTarget(self, action: #selector(registerButtonAction), for: .touchUpInside)
        return button
    }()
    private lazy var forgetThePasswordButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitle("忘记密码？", for: .normal)
        button.setTitleColor(.lxs.hex("#FFFFFF"), for: .normal)
        button.titleLabel?.font = .lxs.font(size: 12)
        button.addTarget(self, action: #selector(forgetThePasswordButtonAction), for: .touchUpInside)
        return button
    }()
    
    private lazy var loginButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setBackgroundImage(UIImage(named: "login_bg_img"), for: .normal)
        button.setBackgroundImage(UIImage(named: "login_bg_img"), for: .normal)
        button.setTitle("登录", for: .normal)
        button.setTitle("登录", for: .selected)
        button.setTitleColor(.lxs.hex("#F7FAFF"), for: .normal)
        button.setTitleColor(.lxs.hex("#F7FAFF"), for: .selected)
        button.titleLabel?.font = .lxs.font(size: 16)
        button.addTarget(self, action: #selector(loginButtonAction), for: .touchUpInside)
        return button
    }()
    private lazy var countryNumBgView: UIView = {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: 70, height: 60))
        view.addSubview(countryNumLabel)
        view.addSubview(lineLabel)
        return view
    }()
    private lazy var countryNumLabel: UILabel = {
        let label = UILabel(frame: CGRect(x: 13, y: 0, width: 30, height: 60))
        label.text = "+86"
        label.textColor = .lxs.hex("#FFFFFF")
        label.font = .lxs.font(size: 14)
        return label
    }()
    private lazy var lineLabel: UILabel = {
        let label = UILabel(frame: CGRect(x: countryNumLabel.frame.maxX + 12, y: (60 - 21) / 2.0, width: 14, height: 21))
        label.text = "丨"
        label.textColor = .lxs.hex("#FFFFFF")
        label.font = .lxs.font(size: 14)
        return label
    }()
    private lazy var getNumButton: UIButton = {
        let button = UIButton(type: .custom)
        button.frame = CGRect(x: 20, y: 0, width: 70, height: 60)
        button.setTitle("获取验证码", for: .normal)
        button.setTitle("获取验证码", for: .selected)
        button.setTitleColor(.lxs.hex("#57A0FF"), for: .normal)
        button.setTitleColor(.lxs.hex("#57A0FF"), for: .selected)
        button.titleLabel?.font = .lxs.font(size: 14)
        button.addTarget(self, action: #selector(getNumButtonAction), for: .touchUpInside)
        return button
    }()
    private lazy var passwordTextFieldRightView: UIView = {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: 111, height: 60))
        view.addSubview(getNumButton)
        return view
    }()
    private lazy var touristModeButton: UIButton = {
        let button = UIButton(type: .custom)
        button.frame = CGRect(x: 20, y: 0, width: 70, height: 60)
        button.setTitle("游客模式", for: .normal)
        button.setTitle("游客模式", for: .selected)
        button.setTitleColor(.lxs.hex("#FFFFFF"), for: .normal)
        button.setTitleColor(.lxs.hex("#FFFFFF"), for: .selected)
        button.titleLabel?.font = .lxs.font(size: 14)
        button.addTarget(self, action: #selector(touristModeButtonAction), for: .touchUpInside)
        return button
    }()
}
